<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>VueRouter—05-API | 小黑博客</title><meta name="keywords" content="JavaScript,Vue"><meta name="author" content="BluesYoung-web"><meta name="copyright" content="BluesYoung-web"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="createRouter(options: RouterOptions) 创建路由示例  RouterOptionshistory 历史模式： createWebHistory(base?: string) H5 模式，需要服务端配置支持 createWebHashHistory(base?: string) hash 模式，无需服务器支持，不利于 SEO createMemoryHistory(">
<meta property="og:type" content="article">
<meta property="og:title" content="VueRouter—05-API">
<meta property="og:url" content="https://bluesyoung-web.gitee.io/blog/2021-08-03-VueRouter%E2%80%9405-API.html">
<meta property="og:site_name" content="小黑博客">
<meta property="og:description" content="createRouter(options: RouterOptions) 创建路由示例  RouterOptionshistory 历史模式： createWebHistory(base?: string) H5 模式，需要服务端配置支持 createWebHashHistory(base?: string) hash 模式，无需服务器支持，不利于 SEO createMemoryHistory(">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://bluesyoung-web.gitee.io/blog/img/vue.jpg">
<meta property="article:published_time" content="2021-08-03T01:49:54.000Z">
<meta property="article:modified_time" content="2021-08-08T08:21:53.395Z">
<meta property="article:author" content="BluesYoung-web">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://bluesyoung-web.gitee.io/blog/img/vue.jpg"><link rel="shortcut icon" href="/blog/img/favicon.png"><link rel="canonical" href="https://bluesyoung-web.gitee.io/blog/2021-08-03-VueRouter%E2%80%9405-API"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: BluesYoung-web","link":"链接: ","source":"来源: 小黑博客","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"top-right"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'VueRouter—05-API',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: false,
  postUpdate: '2021-08-08 16:21:53'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/blog/css/background.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/sviptzk/HexoStaticFile@latest/Hexo/css/custom.min.css"></link><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/blog/img/lufei_siwangningshi.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">153</div></a></div><div class="data-item"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">54</div></a></div><div class="data-item"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">48</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/blog/img/vue.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">小黑博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">VueRouter—05-API</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-08-03T01:49:54.000Z" title="发表于 2021-08-03 09:49:54">2021-08-03</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-08-08T08:21:53.395Z" title="更新于 2021-08-08 16:21:53">2021-08-08</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/Vue/">Vue</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/Vue/VueRouter/">VueRouter</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.2k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>4分钟</span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="createRouter-options-RouterOptions"><a href="#createRouter-options-RouterOptions" class="headerlink" title="createRouter(options: RouterOptions)"></a><code>createRouter(options: RouterOptions)</code></h2><ul>
<li>创建路由示例</li>
</ul>
<h3 id="RouterOptions"><a href="#RouterOptions" class="headerlink" title="RouterOptions"></a><code>RouterOptions</code></h3><h4 id="history"><a href="#history" class="headerlink" title="history"></a><code>history</code></h4><ul>
<li>历史模式：<ul>
<li><code>createWebHistory(base?: string)</code> <code>H5</code> 模式，<strong>需要服务端配置支持</strong></li>
<li><code>createWebHashHistory(base?: string)</code> <code>hash</code> 模式，无需服务器支持，<strong>不利于 <code>SEO</code></strong></li>
<li><code>createMemoryHistory(base?: string)</code> 基于内存的历史记录，<strong>主要用于处理 <code>SSR</code></strong></li>
</ul>
</li>
</ul>
<h4 id="linkActiveClass"><a href="#linkActiveClass" class="headerlink" title="linkActiveClass"></a><code>linkActiveClass</code></h4><ul>
<li>用于激活 <code>RouterLink</code> 的默认类</li>
<li>默认值为 <code>router-link-active</code></li>
</ul>
<h4 id="linkExactActiveClass"><a href="#linkExactActiveClass" class="headerlink" title="linkExactActiveClass"></a><code>linkExactActiveClass</code></h4><ul>
<li>用于精准激活的 <code>RouterLink</code> 的默认类</li>
<li>默认值为 <code>router-link-exact-active</code></li>
</ul>
<h4 id="parseQuery"><a href="#parseQuery" class="headerlink" title="parseQuery"></a><code>parseQuery</code></h4><ul>
<li>用于解析查询的自定义实现</li>
<li><code>parseQuery?: (searchQuery: string) =&gt; Record&lt;string, (string | null)[] | string | null&gt;</code></li>
</ul>
<h4 id="stringifyQuery"><a href="#stringifyQuery" class="headerlink" title="stringifyQuery"></a><code>stringifyQuery</code></h4><ul>
<li>对查询对象进行字符串化的自定义实现</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">stringifyQuery?: (</span><br><span class="line">  query: Record&lt;</span><br><span class="line">    string | number,</span><br><span class="line">    string | number | <span class="literal">null</span> | <span class="literal">undefined</span> | (string | number | <span class="literal">null</span> | <span class="literal">undefined</span>)[]</span><br><span class="line">  &gt;</span><br><span class="line">) =&gt; string</span><br></pre></td></tr></table></figure>

<h4 id="routes"><a href="#routes" class="headerlink" title="routes"></a><code>routes</code></h4><ul>
<li>初始路由表</li>
</ul>
<h4 id="scrollBehavior"><a href="#scrollBehavior" class="headerlink" title="scrollBehavior"></a><code>scrollBehavior</code></h4><ul>
<li>页面切换时的滚动表现(行为)</li>
</ul>
<h2 id="START-LOCATION"><a href="#START-LOCATION" class="headerlink" title="START_LOCATION"></a><code>START_LOCATION</code></h2><ul>
<li><strong>路由所在的初始路由地址</strong></li>
</ul>
<h2 id="Router-属性"><a href="#Router-属性" class="headerlink" title="Router 属性"></a>Router 属性</h2><ul>
<li><code>currentRoute: Ref&lt;RouteLocationNormalized&gt;</code> 当前路由的地址，<strong>只读</strong></li>
<li><code>options: RouterOptions</code> 创建路由传递的原始配置对象，<strong>只读</strong></li>
</ul>
<h3 id="RouteLocationRaw"><a href="#RouteLocationRaw" class="headerlink" title="RouteLocationRaw"></a><code>RouteLocationRaw</code></h3><ul>
<li><strong>用户级路由地址</strong>，可以作为参数传递给 <code>router.push | redirect | replace</code></li>
<li>值可以是<strong>字符串或者对象</strong></li>
<li><strong>path  属性必须编码，params、query、hash 一定不能编码</strong></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 这三种形式是等价的</span></span><br><span class="line">router.push(<span class="string">&#x27;/users/posva#bio&#x27;</span>)</span><br><span class="line">router.push(&#123; <span class="attr">path</span>: <span class="string">&#x27;/users/posva&#x27;</span>, <span class="attr">hash</span>: <span class="string">&#x27;#bio&#x27;</span> &#125;)</span><br><span class="line">router.push(&#123; <span class="attr">name</span>: <span class="string">&#x27;users&#x27;</span>, <span class="attr">params</span>: &#123; <span class="attr">username</span>: <span class="string">&#x27;posva&#x27;</span> &#125;, <span class="attr">hash</span>: <span class="string">&#x27;#bio&#x27;</span> &#125;)</span><br><span class="line"><span class="comment">// 只改变 hash</span></span><br><span class="line">router.push(&#123; <span class="attr">hash</span>: <span class="string">&#x27;#bio&#x27;</span> &#125;)</span><br><span class="line"><span class="comment">// 只改变 query</span></span><br><span class="line">router.push(&#123; <span class="attr">query</span>: &#123; <span class="attr">page</span>: <span class="string">&#x27;2&#x27;</span> &#125; &#125;)</span><br><span class="line"><span class="comment">// 只改变 param</span></span><br><span class="line">router.push(&#123; <span class="attr">params</span>: &#123; <span class="attr">username</span>: <span class="string">&#x27;jolyne&#x27;</span> &#125; &#125;)</span><br></pre></td></tr></table></figure>

<h3 id="RouteLocation"><a href="#RouteLocation" class="headerlink" title="RouteLocation"></a><code>RouteLocation</code></h3><ul>
<li>可以包含重定向记录的解析(<code>RouteLocationRaw</code>)之后的路由地址，其余同下</li>
</ul>
<h3 id="RouteLocationNormalized"><a href="#RouteLocationNormalized" class="headerlink" title="RouteLocationNormalized"></a><code>RouteLocationNormalized</code></h3><ul>
<li><strong>标准化路由地址</strong>，没有任何重定向记录(<code>to | from</code>)</li>
<li><code>fullPath: string</code> 完整路径</li>
<li><code>hash: string</code> 已解码 <code>URL</code> 的 <code>hash</code> 部分，总是以 <code>#</code> 开头，如果没有则为空字符串</li>
<li><code>query: &#123; [key: string]: string | string[] &#125;</code> 从地址中解析出的查询参数对象</li>
<li><code>matched: RouteLocationNormalized[]</code> 与给定地址匹配的标准化路由记录数组</li>
<li><code>meta: RouteMeta</code> 附加从父级到子级合并的所有匹配记录的数据</li>
<li><code>name: string | symbol | undefined | null</code> 路由记录的名称，默认 <code>undefined</code></li>
<li><code>params: &#123; [key: string]: string | string[] &#125;</code> 从动态路径中提取的已解码参数的对象</li>
<li><code>path: string</code> 编码 <code>URL</code> 的 <code>pathname</code> 部分，与路由地址有关</li>
<li><code>redirectedFrom: RouteLocation</code>  重定向的原始地址，默认为 <code>undefined</code></li>
</ul>
<h2 id="Router-方法"><a href="#Router-方法" class="headerlink" title="Router 方法"></a>Router 方法</h2><ul>
<li><code>addRoute(parentName?: string | symbol, route: RouteRecordRaw): () =&gt; void</code> 添加新的路由，返回一个删除钩子函数</li>
<li><code>afterEach(hook: (to, from) =&gt; void): () =&gt; void</code> 导航钩子，每次导航完成之后执行，返回一个删除注册钩子的函数</li>
<li><code>go(n: number)</code> 操作历史记录，相当于 <code>history.go()</code></li>
<li><code>back()</code> 历史回溯，相当于 <code>history.back() | router.go(-1)</code></li>
<li><code>forward()</code> 前进，相当于 <code>history.forward() | router.go(1)</code></li>
<li><code>beforeEach()</code> 添加一个导航守卫，任意导航前执行，返回一个删除注册的钩子函数</li>
<li><code>beforeResolve()</code> 添加一个导航守卫，在导航解析之前执行(组件已被获取，其他导航守卫也已成功)，返回一个删除注册的钩子函数</li>
<li><code>getRoutes()</code> 获取所有的路由</li>
<li><code>hasRoute()</code> 判断是否存在指定名称的路由</li>
<li><code>isReady()</code> 返回一个 <code>Promise</code> 表示其解析状态</li>
<li><code>onError()</code> 添加一个错误处理程序，导航期间所有未捕获的错误都会传入其中进行处理</li>
<li><code>push()</code> 导航到指定地址</li>
<li><code>replace()</code> 通过替换当前地址的方式导航到指定地址</li>
<li><code>removeRoute()</code> 删除现有路由</li>
<li><code>resolve()</code> 返回路由地址的标准化版本</li>
</ul>
<h3 id="RouteRecordRaw"><a href="#RouteRecordRaw" class="headerlink" title="RouteRecordRaw"></a><code>RouteRecordRaw</code></h3><ul>
<li><strong>原始路由记录</strong>：<ul>
<li>单一视图记录，有一个 <code>component</code> 配置</li>
<li>多视图记录(命名视图)，有一个 <code>components</code> 配置</li>
<li>重定向记录，没有<code>component</code> 或者 <code>components</code> 配置</li>
</ul>
</li>
<li><code>path: string</code> 路由地址，应该以 <code>/</code> 开头，子路由除外</li>
<li><code>redirect?: RouteLocationRaw | (to) =&gt; RouteLocationNormalized</code> 重定向地址</li>
<li><code>children?: RouteRecordRaw[]</code> 数组</li>
<li><code>alias?: string | string[]</code> 别名</li>
<li><code>name?: string | symbol</code> <strong>名称，唯一标识</strong></li>
<li><code>beforeEnter?: NavigationGuard | NavigationGard[]</code> 专属导航守卫</li>
<li><code>props? boolean | &#123; [prop: string]: any &#125; | (to) =&gt; &#123; [prop: string]: any &#125;</code> 参数表现</li>
<li><code>meta?: RouteMeta</code> 自定义元数据</li>
</ul>
<h3 id="RouteRecordNormalized"><a href="#RouteRecordNormalized" class="headerlink" title="RouteRecordNormalized"></a><code>RouteRecordNormalized</code></h3><ul>
<li>路由记录的标准化版本</li>
<li><code>aliasOf: RouterRecordNormalized | undefined</code> 表示此记录是否是原始记录(<code>undefined</code>)</li>
<li><code>beforeEnter: NavigationGuard</code> 专属导航守卫</li>
<li><code>children: RouterRecordNormalized[]</code> 子路由</li>
<li><code>components: &#123; [prop: string]: Component &#125;</code> 命名视图对象，默认包含键名<code>default</code></li>
<li><code>meta: RouteMeta</code> 自定义元数据</li>
<li><code>name: string | symbol | undefined</code> 路由记录的名称</li>
<li><code>path: string</code> 路由的标准化路径(完整路径)</li>
<li><code>props: &#123; [prop: string]: boolean | Function | &#123; [prop: string]: any &#125; &#125;</code> 与 <code>components</code> 对应的表现</li>
<li><code>redirect: RouteLocationRaw</code> 重定向记录</li>
</ul>
<h3 id="NavigationFailure"><a href="#NavigationFailure" class="headerlink" title="NavigationFailure"></a><code>NavigationFailure</code></h3><ul>
<li><code>from</code> 发起导航的标准化路由记录</li>
<li><code>to</code> 导航的目标标准化路由记录</li>
<li><code>type</code> 失败的类型</li>
</ul>
<h2 id="组件注入"><a href="#组件注入" class="headerlink" title="组件注入"></a>组件注入</h2><ul>
<li>主要针对 <code>Vue2</code> 的写法</li>
<li><code>this.$router</code></li>
<li><code>this.$route</code></li>
<li><code>beforeRouteEnter</code></li>
<li><code>beforeRouteUpdate</code></li>
<li><code>beforeRouteLeave</code></li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">BluesYoung-web</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://bluesyoung-web.gitee.io/blog/2021-08-03-VueRouter%E2%80%9405-API.html">https://bluesyoung-web.gitee.io/blog/2021-08-03-VueRouter%E2%80%9405-API.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://BluesYoung-web.gitee.io/blog" target="_blank">小黑博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/JavaScript/">JavaScript</a><a class="post-meta__tags" href="/blog/tags/Vue/">Vue</a></div><div class="post_share"></div></div><div class="post-reward"><div class="reward-button button--animated"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/blog/img/wx.png" target="_blank"><img class="post-qr-code-img" src="/blog/img/wx.png" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/blog/img/zfb.jpg" target="_blank"><img class="post-qr-code-img" src="/blog/img/zfb.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blog/2021-04-28-Vue3项目使用tailwindcss.html" title="Vue3项目使用tailwindcss"><img class="cover" src="/blog/img/vue.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-04-28</div><div class="title">Vue3项目使用tailwindcss</div></div></a></div><div><a href="/blog/2021-05-29-Vue3项目使用windicss.html" title="Vue3项目使用windicss"><img class="cover" src="/blog/img/vue.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-05-29</div><div class="title">Vue3项目使用windicss</div></div></a></div><div><a href="/blog/2021-07-27-VueRouter—02-进阶-导航守卫.html" title="VueRouter—02.进阶-导航守卫"><img class="cover" src="/blog/img/vue.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-07-27</div><div class="title">VueRouter—02.进阶-导航守卫</div></div></a></div><div><a href="/blog/2021-07-22-VueRouter—01-基础.html" title="VueRouter—01.基础"><img class="cover" src="/blog/img/vue.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-07-22</div><div class="title">VueRouter—01.基础</div></div></a></div><div><a href="/blog/2021-08-02-VueRouter—03-进阶.html" title="VueRouter—03-进阶"><img class="cover" src="/blog/img/vue.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-08-02</div><div class="title">VueRouter—03-进阶</div></div></a></div><div><a href="/blog/2021-08-02-VueRouter—04-标签.html" title="VueRouter—04-标签"><img class="cover" src="/blog/img/vue.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-08-02</div><div class="title">VueRouter—04-标签</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/blog/img/lufei_siwangningshi.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">BluesYoung-web</div><div class="author-info__description">一枚不善言辞的技术宅</div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">153</div></a></div><div class="card-info-data-item"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">54</div></a></div><div class="card-info-data-item"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">48</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/BluesYoung-web"><i class="fab fa-gitee"></i><span>点个小星星吧</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/BluesYoung-web" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:15171255945@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="sticky_layout"><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2021-08-24-%E7%A7%BB%E5%8A%A8%E7%AB%AF%E8%BD%AF%E9%94%AE%E7%9B%98%E5%AF%BC%E8%87%B4%E5%B8%83%E5%B1%80%E9%94%99%E4%B9%B1.html" title="移动端软键盘导致布局错乱"><img src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="移动端软键盘导致布局错乱"/></a><div class="content"><a class="title" href="/blog/2021-08-24-%E7%A7%BB%E5%8A%A8%E7%AB%AF%E8%BD%AF%E9%94%AE%E7%9B%98%E5%AF%BC%E8%87%B4%E5%B8%83%E5%B1%80%E9%94%99%E4%B9%B1.html" title="移动端软键盘导致布局错乱">移动端软键盘导致布局错乱</a><time datetime="2021-08-24T10:01:20.000Z" title="发表于 2021-08-24 18:01:20">2021-08-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2021-08-16-Linux%E5%AE%89%E8%A3%85you-get.html" title="Linux安装you-get"><img src="/blog/img/shumeipai_cover.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Linux安装you-get"/></a><div class="content"><a class="title" href="/blog/2021-08-16-Linux%E5%AE%89%E8%A3%85you-get.html" title="Linux安装you-get">Linux安装you-get</a><time datetime="2021-08-16T08:07:31.000Z" title="发表于 2021-08-16 16:07:31">2021-08-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2021-08-16-git%E5%88%A0%E9%99%A4%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93%E7%9A%84%E6%8F%90%E4%BA%A4.html" title="git删除远程仓库的提交"><img src="/blog/img/skill.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="git删除远程仓库的提交"/></a><div class="content"><a class="title" href="/blog/2021-08-16-git%E5%88%A0%E9%99%A4%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93%E7%9A%84%E6%8F%90%E4%BA%A4.html" title="git删除远程仓库的提交">git删除远程仓库的提交</a><time datetime="2021-08-16T07:52:02.000Z" title="发表于 2021-08-16 15:52:02">2021-08-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2021-08-13-%E5%B0%86pad%E6%89%A9%E5%B1%95%E4%B8%BA%E7%94%B5%E8%84%91%E7%9A%84%E5%89%AF%E5%B1%8F.html" title="将pad扩展为电脑的副屏"><img src="/blog/img/skill.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="将pad扩展为电脑的副屏"/></a><div class="content"><a class="title" href="/blog/2021-08-13-%E5%B0%86pad%E6%89%A9%E5%B1%95%E4%B8%BA%E7%94%B5%E8%84%91%E7%9A%84%E5%89%AF%E5%B1%8F.html" title="将pad扩展为电脑的副屏">将pad扩展为电脑的副屏</a><time datetime="2021-08-13T11:44:04.000Z" title="发表于 2021-08-13 19:44:04">2021-08-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2021-08-11-Linux%E5%90%AF%E7%94%A8SSH%E6%9C%8D%E5%8A%A1.html" title="Linux启用SSH服务"><img src="/blog/img/shumeipai_cover.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Linux启用SSH服务"/></a><div class="content"><a class="title" href="/blog/2021-08-11-Linux%E5%90%AF%E7%94%A8SSH%E6%9C%8D%E5%8A%A1.html" title="Linux启用SSH服务">Linux启用SSH服务</a><time datetime="2021-08-11T10:50:04.000Z" title="发表于 2021-08-11 18:50:04">2021-08-11</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By BluesYoung-web</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/blog/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>if (document.getElementsByClassName('mermaid').length) {
  if (window.mermaidJsLoad) mermaid.init()
  else {
    getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(() => {
      window.mermaidJsLoad = true
      mermaid.initialize({
        theme: 'default',
      })
      false && mermaid.init()
    })
  }
}</script></div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script> <script src="/blog/js/xkTool.js"></script> <script src="/blog/js/mySelf.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script></div></body></html>